﻿/*              Copyright(c) 2008 Dave Sexton              *
 * http://www.codeplex.com/DocProject                      *
 * http://www.codeplex.com/DocProject/Project/License.aspx *
 ***********************************************************/
using System;
using System.Collections.Generic;
using System.Text;
using System.ComponentModel;
using System.Xml;

namespace DaveSexton.DocToMaml.Maml
{
	internal enum MamlLinkType
	{
		XLink,
		CodeEntityReference,
		ExternalLink
	}

	internal struct MamlLink
	{
		#region Public Properties
		public MamlLinkType LinkType { get; private set; }

		public string Value { get; private set; }

		public string Text { get; private set; }

		public string AlternateText { get; private set; }
		#endregion

		#region Private / Protected
		#endregion

		#region Constructors
		/// <summary>
		/// Constructs a new instance of the <see cref="MamlLink" /> class.
		/// </summary>
		public MamlLink(MamlLinkType linkType, string value, string text, string alternateText)
			: this()
		{
			if (!Enum.IsDefined(typeof(MamlLinkType), linkType))
				throw new InvalidEnumArgumentException("linkType", (int) linkType, typeof(MamlLinkType));

			LinkType = linkType;
			Value = value;
			Text = text;
			AlternateText = alternateText;
		}
		#endregion

		#region Methods
		public void Write(XmlWriter writer)
		{
			switch (LinkType)
			{
				case MamlLinkType.XLink:
					writer.WriteStartElement("link");
					writer.WriteAttributeString("xlink", "href", "http://www.w3.org/1999/xlink", Value);
					writer.WriteEndElement();
					break;
				case MamlLinkType.CodeEntityReference:
					writer.WriteElementString("codeEntityReference", Value);
					break;
				case MamlLinkType.ExternalLink:
					writer.WriteStartElement("externalLink");

					writer.WriteStartElement("linkText");
					writer.WriteString(Text);
					writer.WriteEndElement();

					if (!string.IsNullOrEmpty(AlternateText))
					{
						writer.WriteStartElement("linkAlternateText");
						writer.WriteString(AlternateText);
						writer.WriteEndElement();
					}

					writer.WriteStartElement("linkUri");
					writer.WriteString(Value);
					writer.WriteEndElement();

					writer.WriteEndElement();
					break;
			}
		}
		#endregion
	}
}
